<?php
/**
 * Jamnews Admin
 *
 * Main admin file which loads all settings panels and sets up admin menus.
 *
 * @author 		VoAnhKiet
 * @category 	Admin
 * @package 	Jamnews/Admin
 * @version     1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Setup the Admin menu in WordPress
 *
 * @access public
 * @return void
 */
function jamnews_admin_menu() {
	global $menu, $jamnews;
	if(current_user_can("manager_jamnews")){
	
		// Setting page
		$main_page = add_menu_page( __( 'Jam Settings', 'jamnews' ), __( 'Jam Options', 'jamnews' ), 'manage_options', 'jamnews' , 'jamnews_admin_setting', $jamnews->plugin_url()."/images/icon-jamnews.png", null );
		// List post type
		add_submenu_page( 'jamnews', __( 'Post Type', 'jamnews' ), __( 'Post Type', 'jamnews' ) , 'manager_jamnews', 'jamnews_posttype' , 'jamnews_admin_posttype' );
		// List taxonomy
		add_submenu_page( 'jamnews', __( 'Taxonomy', 'jamnews' ), __( 'Taxonomy', 'jamnews' ) , 'manager_jamnews', 'jamnews_taxonomy' , 'jamnews_admin_taxonomy' );
		
	}
}
add_action('admin_menu', 'jamnews_admin_menu', 9);

/**
 * Include and display setting page
 *
 * @access public
 * @return void
 */
function jamnews_admin_setting() {
	include_once( 'jamnews-admin-setting.php' );
	jamnews_manager_setting();
}


/**
 * Include and display post type
 *
 * @access public
 * @return void
 */
function jamnews_admin_posttype() {
	include_once( 'jamnews-admin-posttype.php' );
	jamnews_manager_posttypes();
}

/**
 * Include and display the taxonomy
 *
 * @access public
 * @return void
 */
function jamnews_admin_taxonomy() {
	include_once( 'jamnews-admin-taxonomy.php' );
	jamnews_manager_taxonomy();
}


/**
 * Include admin scripts and styles.
 *
 * @access public
 * @return void
 */
function jamnews_admin_scripts() {
    global $jamnews, $pagenow, $post, $wp_query;
    
    $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
    
    $screen = get_current_screen();
    $jn_screen_id = strtolower( __( 'Jamnews', 'jamnews' ) );
	
	wp_register_script( 'bootstrap', $jamnews->plugin_url() . '/js/bootstrap.js' );
	wp_register_script( 'chosen', $jamnews->plugin_url() . '/js/chosen/chosen.jquery.js', array( 'jquery' ), $jamnews->version, true );
	wp_register_script( 'tiptip', $jamnews->plugin_url() . '/js/jquery-tiptip/jquery.tipTip.js', array( 'jquery' ), $jamnews->version, true );
	wp_register_script( 'validate', $jamnews->plugin_url() . '/js/jquery.validate.js', array( 'jquery' ), $jamnews->version, true );
    wp_register_script( 'jamnews', $jamnews->plugin_url() . '/js/jamnews.js' );
	
    if( in_array($screen->id, apply_filters('jamnews_screen_ids', array( 'toplevel_page_jamnews', 'jam-options_page_jamnews_posttype', 'jam-options_page_jamnews_taxonomy' ) ) ) ){
		wp_enqueue_media();
        wp_enqueue_script( 'bootstrap' );
		wp_enqueue_script( 'chosen' );
		wp_enqueue_script( 'tiptip' );
		wp_enqueue_script( 'validate' );
		wp_enqueue_script( 'jquery-ui-sortable' );
		wp_enqueue_script( 'jquery-ui-draggable' );	 
		wp_enqueue_script( 'jquery-ui-droppable' );	 
        wp_enqueue_script( 'jamnews' );
    }
}
add_action( 'admin_enqueue_scripts', 'jamnews_admin_scripts' );

/**
 * Queue admin menu icons CSS.
 *
 * @access public
 * @return void
 */
function jamnews_admin_menu_styles() {
	global $jamnews;
    $screen = get_current_screen();    
    if( in_array($screen->id, apply_filters('jamnews_screen_ids', array( 'toplevel_page_jamnews', 'jam-options_page_jamnews_posttype', 'jam-options_page_jamnews_taxonomy' ) ) ) ){
		wp_enqueue_style( 'jamnews_bootstrap_styles', $jamnews->plugin_url() . '/css/bootstrap.css' );
		wp_enqueue_style( 'jamnews_bootstrap_responsive_styles', $jamnews->plugin_url() . '/css/bootstrap-responsive.css' );
		wp_enqueue_style( 'font-awesome', $jamnews->plugin_url() . '/css/font-awesome.css' );
		wp_enqueue_style( 'jamnews_chose_styles', $jamnews->plugin_url() . '/css/chosen.css' );
		wp_enqueue_style( 'jquery-ui-style', 'http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css');
    	wp_enqueue_style( 'jamnews_admin_menu_styles', $jamnews->plugin_url() . '/css/jamnews.css' );
    }
}
add_action( 'admin_print_styles', 'jamnews_admin_menu_styles' );

/**
 * Output admin fields.
 *
 * Loops though the jamnews options array and outputs each field.
 *
 * @access public
 * @param array $options Opens array to output
 * @return void
 */
function jamnews_admin_fields( $options ) {
	global $jamnews;

	foreach($options as $value){
		if ( ! isset( $value['type'] ) ) continue;
		if ( ! isset( $value['id'] ) ) $value['id'] = '';
		if ( ! isset( $value['name'] ) ) $value['name'] = '';
		if ( ! isset( $value['title'] ) ) $value['title'] = '';
		if ( ! isset( $value['class'] ) ) $value['class'] = '';
		if ( ! isset( $value['css'] ) ) $value['css'] = '';
		if ( ! isset( $value['default'] ) ) $value['default'] = '';
		if ( ! isset( $value['desc'] ) ) $value['desc'] = '';
		if ( ! isset( $value['desc_tip'] ) ) $value['desc_tip'] = false;
		
		// Custom attribute handling
		$custom_attributes = array();

		if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) )
			foreach ( $value['custom_attributes'] as $attribute => $attribute_value )
				$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';

		// Description handling
		if ( $value['desc_tip'] === true ) {
			$description = '';
			$tip = $value['desc'];
		} elseif ( ! empty( $value['desc_tip'] ) ) {
			$description = $value['desc'];
			$tip = $value['desc_tip'];
		} elseif ( ! empty( $value['desc'] ) ) {
			$description = $value['desc'];
			$tip = '';
		} else {
			$description = $tip = '';
		}

		if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ) ) ) {
			$description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
		} elseif ( $description ) {
			$description = '<span class="description">' . wp_kses_post( $description ) . '</span>';
		}

		if ( $tip && in_array( $value['type'], array( 'checkbox' ) ) ) {

			$tip = '<p class="description">' . $tip . '</p>';

		} elseif ( $tip ) {

			$tip = '<img class="help_tip" data-tip="' . esc_attr( $tip ) . '" src="' . $jamnews->plugin_url() . '/images/help.png" height="16" width="16" />';

		}
		
		switch($value['type']){
			// Section Titles
			case 'title':
				if ( ! empty( $value['title'] ) ) echo '<h2>' . esc_html( $value['title'] ) . '</h2>';
				if ( ! empty( $value['desc'] ) ) echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) );
				echo '<table class="form-table">'. "\n\n";
				if ( ! empty( $value['id'] ) ) do_action( 'jamnews_admin_' . sanitize_title( $value['id'] ) );
			break;

			// Section Ends
			case 'sectionend':
				if ( ! empty( $value['id'] ) ) do_action( 'jamnews_admin_' . sanitize_title( $value['id'] ) . '_end' );
				echo '</table>';
				if ( ! empty( $value['id'] ) ) do_action( 'jamnews_admin_' . sanitize_title( $value['id'] ) . '_after' );
			break;
			
			// Standard text inputs and subtypes like 'number'
			case 'text':
			case 'email':
			case 'number':
			case 'color' :
			case 'password' :

				$type 			= $value['type'];
				$class 			= '';
				$option_value 	= jamnews_get_option( $value['id'], $value['default'] );

				if ( $value['type'] == 'color' ) {
					$type = 'text';
					$value['class'] .= 'colorpick';
					$description .= '<div id="colorPickerDiv_' . esc_attr( $value['id'] ) . '" class="colorpickdiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"></div>';
				}

				?><tr valign="top">
					<th scope="row" class="titledesc">
						<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
						<?php echo $tip; ?>
					</th>
					<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
						<input
							name="<?php echo esc_attr( $value['name'] ); ?>"
							id="<?php echo esc_attr( $value['id'] ); ?>"
							type="<?php echo esc_attr( $type ); ?>"
							style="<?php echo esc_attr( $value['css'] ); ?>"
							value="<?php echo esc_attr( $option_value ); ?>"
							class="<?php echo esc_attr( $value['class'] ); ?>"
							<?php echo implode( ' ', $custom_attributes ); ?>
							/> <?php echo $description; ?>
					</td>
				</tr><?php
			break;
			
			// Textarea
			case 'textarea':

				$option_value 	= jamnews_get_option( $value['id'], $value['default'] );

				?><tr valign="top">
					<th scope="row" class="titledesc">
						<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
						<?php echo $tip; ?>
					</th>
					<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
						<?php echo $description; ?>

						<textarea
							name="<?php echo esc_attr( $value['id'] ); ?>"
							id="<?php echo esc_attr( $value['id'] ); ?>"
							style="<?php echo esc_attr( $value['css'] ); ?>"
							class="<?php echo esc_attr( $value['class'] ); ?>"
							<?php echo implode( ' ', $custom_attributes ); ?>
							><?php echo esc_textarea( $option_value );  ?></textarea>
					</td>
				</tr><?php
			break;

			// Select boxes
			case 'select' :
			case 'multiselect' :

				$option_value 	= jamnews_get_option( $value['id'], $value['default'] );
				
				?><tr valign="top">
					<th scope="row" class="titledesc">
						<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
						<?php echo $tip; ?>
					</th>
					<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
						<select
							name="<?php echo esc_attr( $value['id'] ); ?><?php if ( $value['type'] == 'multiselect' ) echo '[]'; ?>"
							id="<?php echo esc_attr( $value['id'] ); ?>"
							style="<?php echo esc_attr( $value['css'] ); ?>"
							class="<?php echo esc_attr( $value['class'] ); ?>"
							<?php echo implode( ' ', $custom_attributes ); ?>
							<?php if ( $value['type'] == 'multiselect' ) echo 'multiple="multiple"'; ?>
							>
							<?php
								foreach ( $value['options'] as $key => $val ) {
									?>
									<option value="<?php echo esc_attr( $key ); ?>" <?php

										if ( is_array( $option_value ) )
											selected( in_array( $key, $option_value ), true );
										else
											selected( $option_value, $key );

									?>><?php echo $val ?></option>
									<?php
								}
							?>
					   </select> <?php echo $description; ?>
					</td>
				</tr><?php
			break;

			// Radio inputs
			case 'radio' :

				$option_value 	= jamnews_get_option( $value['id'], $value['default'] );

				?><tr valign="top">
					<th scope="row" class="titledesc">
						<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
						<?php echo $tip; ?>
					</th>
					<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
						<fieldset>
							<?php echo $description; ?>
							<ul>
							<?php
								foreach ( $value['options'] as $key => $val ) {
									?>
									<li>
										<label><input
											name="<?php echo esc_attr( $value['id'] ); ?>"
											value="<?php echo $key; ?>"
											type="radio"
											style="<?php echo esc_attr( $value['css'] ); ?>"
											class="<?php echo esc_attr( $value['class'] ); ?>"
											<?php echo implode( ' ', $custom_attributes ); ?>
											<?php checked( $key, $option_value ); ?>
											/> <?php echo $val ?></label>
									</li>
									<?php
								}
							?>
							</ul>
						</fieldset>
					</td>
				</tr><?php
			break;

			// Checkbox input
			case 'checkbox' :

				$option_value 	= jamnews_get_option( $value['id'], $value['default'] );

				if ( ! isset( $value['hide_if_checked'] ) ) $value['hide_if_checked'] = false;
				if ( ! isset( $value['show_if_checked'] ) ) $value['show_if_checked'] = false;

				if ( ! isset( $value['checkboxgroup'] ) || ( isset( $value['checkboxgroup'] ) && $value['checkboxgroup'] == 'start' ) ) {
					?>
					<tr valign="top" class="<?php
						if ( $value['hide_if_checked'] == 'yes' || $value['show_if_checked']=='yes') echo 'hidden_option';
						if ( $value['hide_if_checked'] == 'option' ) echo 'hide_options_if_checked';
						if ( $value['show_if_checked'] == 'option' ) echo 'show_options_if_checked';
					?>">
					<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th>
					<td class="forminp forminp-checkbox">
						<fieldset>
					<?php
				} else {
					?>
					<fieldset class="<?php
						if ( $value['hide_if_checked'] == 'yes' || $value['show_if_checked'] == 'yes') echo 'hidden_option';
						if ( $value['hide_if_checked'] == 'option') echo 'hide_options_if_checked';
						if ( $value['show_if_checked'] == 'option') echo 'show_options_if_checked';
					?>">
					<?php
				}

				?>
					<legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ) ?></span></legend>

					<label for="<?php echo $value['id'] ?>">
					<input
						name="<?php echo esc_attr( $value['name'] ); ?>"
						id="<?php echo esc_attr( $value['id'] ); ?>"
						type="checkbox"
						value="1"
						<?php checked( $option_value, 'yes'); ?>
						<?php echo implode( ' ', $custom_attributes ); ?>
					/> <?php echo wp_kses_post( $value['desc'] ) ?></label> <?php echo $tip; ?>
				<?php

				if ( ! isset( $value['checkboxgroup'] ) || ( isset( $value['checkboxgroup'] ) && $value['checkboxgroup'] == 'end' ) ) {
					?>
						</fieldset>
					</td>
					</tr>
					<?php
				} else {
					?>
					</fieldset>
					<?php
				}

			break;

			// Image width settings
			case 'image_width' :

				$width 	= jamnews_get_option( $value['id'] . '[width]', $value['default']['width'] );
				$height = jamnews_get_option( $value['id'] . '[height]', $value['default']['height'] );
				$crop 	= checked( 1, jamnews_get_option( $value['id'] . '[crop]', $value['default']['crop'] ), false );

				?><tr valign="top">
					<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
					<td class="forminp">

						<?php _e( 'Width', 'jamnews' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>[width]" id="<?php echo esc_attr( $value['id'] ); ?>-width" type="text" size="3" value="<?php echo $width; ?>" />

						<?php _e( 'Height', 'jamnews' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>[height]" id="<?php echo esc_attr( $value['id'] ); ?>-height" type="text" size="3" value="<?php echo $height; ?>" />

						<label><?php _e( 'Hard Crop', 'jamnews' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>[crop]" id="<?php echo esc_attr( $value['id'] ); ?>-crop" type="checkbox" <?php echo $crop; ?> /></label>

						</td>
				</tr><?php
			break;
			
			// Upload
			case 'upload':
				$option_value 	= jamnews_get_option( $value['id'], $value['default'] );
				?><tr valign="top">
					<th scope="row" class="titledesc">
						<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
						<?php echo $tip; ?>
					</th>
					<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
						<input
							name="<?php echo esc_attr( $value['id'] ); ?>"
							id="<?php echo esc_attr( $value['id'] ); ?>"
							type="<?php echo esc_attr( 'hidden' ); ?>"
							style="<?php echo esc_attr( $value['css'] ); ?>"
							value="<?php echo esc_attr( $option_value ); ?>"
							class="<?php echo esc_attr( $value['class'] ); ?>"
							<?php echo implode( ' ', $custom_attributes ); ?>
							/><span class="review-icon"></span> <a href="#" class="btn btn-small btn-media-upload"><i class="icon icon-picture color-green"></i></a> <?php echo $description; ?><br/>
							<div class="img-preview"><?php 
								if($option_value!=""){ list($thumb) = wp_get_attachment_image_src($option_value,"medium"); ?>
									<img class="thumbnail" src="<?php echo $thumb;?>" /> 
									<a href="#"  class='remove-upload-image' style="position: absolute; top: -10px; right: -8px;"><i class="icon-minus-sign icon-large color-red"></i></a><?php
								}?>
							</div>
					</td>
				</tr><?php
			default: 
				do_action( 'jamnews_admin_field_' . $value['type'], $value );
			break;
		}
	}
	}


	/**
	* Get a setting from the settings API.
	*
	* @access public
	* @param mixed $option
	* @return void
	*/
	function jamnews_get_option( $option_name, $default = '' ) {
	// Array value
	if ( strstr( $option_name, '[' ) ) {

		parse_str( $option_name, $option_array );

		// Option name is first key
		$option_name = current( array_keys( $option_array ) );

		// Get value
		$option_values = get_option( $option_name, '' );

		$key = key( $option_array[ $option_name ] );

		if ( isset( $option_values[ $key ] ) )
			$option_value = $option_values[ $key ];
		else
			$option_value = null;

	// Single value
	} else {
		$option_value = get_option( $option_name, null );
	}

	if ( is_array( $option_value ) )
		$option_value = array_map( 'stripslashes', $option_value );
	elseif ( ! is_null( $option_value ) )
		$option_value = stripslashes( $option_value );

	return $option_value === null ? $default : $option_value;
}

/**
	* Show request in field each the element form
	*
	* @access public
	* @param mixed $option
	* @return void
	*/
function _val($key){
	return(isset($_REQUEST[$key])? $_REQUEST[$key]:"");
}
?>